package com.wn.network.interceptor

import com.blankj.utilcode.util.NetworkUtils
import com.wn.network.error.ERROR
import com.wn.network.error.NoNetWorkException
import okhttp3.Interceptor
import okhttp3.Response
import javax.inject.Inject
import javax.inject.Singleton

/**
 * @Author: wn
 * @Date: 2024/8/5 16:13
 * @Description: 网络状态拦截器
 */
@Singleton
class NetworkInterceptor @Inject constructor() : Interceptor {
  override fun intercept(chain: Interceptor.Chain): Response {
    // 网络是否连接  并且 网络是否可用
    if (NetworkUtils.isConnected() && NetworkUtils.isAvailable()) {
      val request = chain.request()
      return chain.proceed(request)
    } else {
      throw NoNetWorkException(ERROR.NETWORK_ERROR)
    }
  }
}